package com.songtech.server.utils;

import com.songtech.server.constant.Calculate;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

/**
 * Create By YINN on 2018/4/11 19:41
 * Description :
 */
public class CalculationUtils {
    private static double EARTH_RADIUS = 6378.137;// 单位千米

    private static int DECIMAL_DIGITS = 6;

    private static int RADIX_16 = 16;

    private static final double KNOT_TO_MS = 0.514; //转换参数

    private static final int SPEED_FACTOR = Calculate.SPEED_FACTOR;

    /**
     * 角度弧度计算公式 rad:(). <br/>
     *
     * 360度=2π π=Math.PI
     *
     * x度 = x*π/360 弧度
     *
     * @author
     * @param
     * @return
     */
    private static double getRadian(double degree) {
        return degree * Math.PI / 180.0;
    }

    /**
     * 根据经纬度计算两点之间的距离 GetDistance:(). <br/>
     *
     *
     * @author chiwei
     * @param lat1
     *            1点的纬度
     * @param lng1
     *            1点的经度
     * @param lat2
     *            2点的纬度
     * @param lng2
     *            2点的经度
     * @return 距离 单位 米
     */
    public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = getRadian(lat1);
        double radLat2 = getRadian(lat2);
        double a = radLat1 - radLat2;// 两点纬度差
        double b = getRadian(lng1) - getRadian(lng2);// 两点的经度差
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1)
                * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        return s * 1000;
    }


    /**
     *
     * 协议中DDMM.MMMM（度分秒格式转化为）DD.DDDDDD（小数形式）
     * **/
    public static double latLonParse(String str){

        int len = str.length();

        String interger = str.substring(0,len - 7);

        String decimal = str.substring(len - 7);

        double dc = Double.valueOf(decimal);

        double body = Double.valueOf(interger) + dc / 60;

        //获取格式化对象
        NumberFormat format = NumberFormat.getNumberInstance();

        format.setMaximumFractionDigits(DECIMAL_DIGITS);

        //设置四舍五入
        format.setRoundingMode(RoundingMode.UP);

        String value  = format.format(body);

        return Double.valueOf(value);

    }


    /**
     * 获取电量 0-100的整数
     * */
    public static String getDumpEnergy(String str){

        //获取十进制电压
        int presentV = Integer.parseInt(str,RADIX_16);

        double dumpEnergy = (((double) presentV) * 5.6/ 1024) * 100 /3.6;

        if (dumpEnergy > 100D){
            dumpEnergy = 100D;
        }

        return String.valueOf((int) Math.rint(dumpEnergy));
    }

    /**
     * 分解equ_status
     */
    public static String getAlertInfo(String in){

       String binaryStr = Integer.toBinaryString(Long.valueOf(in,RADIX_16).intValue());

        return binaryStr;
    }

    /**
     * 速度转换公式
     * 1节转换为m/s
     */
    public static int knotTransToMS(double speed){

        return Double.valueOf(speed * KNOT_TO_MS * SPEED_FACTOR).intValue();
    }


    public static long getLatLon(double data){

        return (long) (data * Calculate.LON_LAT_FACTOR) ;

    }

}
